﻿// ***********************************************************************
// Assembly         : OSS.Earth
// Author           : tianteng
// Created          : 03-16-2018
//
// Last Modified By : tianteng
// Last Modified On : 03-16-2018
// ***********************************************************************
// <copyright file="AddModelCmd.cs" company="北京智帆高科科技有限公司">
//     Copyright ©  2017
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.Windows.Forms;
using OsgEarth;
using Osg;
using ZFGK.Addins;
using ZFGK.IO;
using ZFGK.OSG.UI;
using ZFGK.osgEarth.Utility;
using ZFGK.OSG.Utility;
using ZFGK.osgEarth.UI;
using ZFGK.Utility;
using ZFGK.WinForms.Utility;

namespace OSS.Earth.Commands.ModelCmds
{
    /// <summary>
    /// Class AddModelCmd.
    /// </summary>
    /// <seealso cref="ZFGK.Addins.IExtendCommand" />
    public class ModelAddCmd : IExtendCommand
    {
        /// <summary>
        /// Executes the specified command data.
        /// </summary>
        /// <param name="cmdData">The command data.</param>
        /// <param name="message">The message.</param>
        /// <returns>CmdResult.</returns>
        public CmdResult Execute(ExtendCmdData cmdData, ref string message)
        {
            var osgEarthViewForm = cmdData.ViewForm as OsgEarthViewForm;
            var osgEarthViewCtrl = osgEarthViewForm.OsgEarthViewCtrl;
            var actionUtility = osgEarthViewCtrl.ActionUtility;

            string srcFileName;
            if (!DialogUtil.OpenOSG(out srcFileName))
                return CmdResult.Cancel;

            string hashCode = FileUtil.GetHashCode(srcFileName);
            //var node = osgDB.osgDB.readNodeFile(srcFileName);
            var node = ZfModelCache.GetNode(srcFileName, hashCode);
            if (!node.IsValid)
            {
                ZfMessageUtil.ShowError("模型读取失败!");
                return CmdResult.Cancel;
            }
            OsgEarth.Registry.shaderGenerator().run(node);

            bool bClosedLighting = MessageBox.Show("是否关闭光照？", "插入模型", MessageBoxButtons.YesNo) == DialogResult.Yes;

            if (bClosedLighting)
                node.getOrCreateStateSet().SetLighting(false);

            var matix = Matrixd.scale(1, 1, 1);
            var matrixTransform = new MatrixTransform(matix);
            matrixTransform.addChild(node);

            Vec3d mapPt = null, mapPt2 = null;
            // 选择插入点
            if (!actionUtility.PickEarthPoint("放置模型", matrixTransform, out mapPt))
                return CmdResult.Cancel;
            if (!actionUtility.PickEarthPointLastex("旋转模型", mapPt, matrixTransform, out mapPt2))
                return CmdResult.Cancel;

            matrixTransform.setMatrix(matix * Matrixd.rotate(mapPt.Rotation(mapPt2), new Vec3d(0, 0, 1)));
            
            var geoTransform = new GeoTransform();
            geoTransform.setPosition(new GeoPoint(osgEarthViewCtrl.OsgEarthObj.MapNode.getMapSRS(), mapPt));
            geoTransform.addChild(matrixTransform);

            osgEarthViewCtrl.OsgEarthObj.AddOrReplaceModel("Models", geoTransform, srcFileName);

            return CmdResult.Succeed;
        }
    }
}
